// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:'',
    password:'',
    // 注册
    isRegister:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      isRegister:options.isRegister == 0
    })
    console.log('onload');
  },
  // 自定义函数
  // 事件对象
  getInputVal(e){
    // 获取input的值
    var type = e.currentTarget.dataset.type
    // 分清楚 是 userName  or  password
    if(type == 'account'){
        this.setData({
          username:e.detail.value
        })
    }else if(type == 'password'){
      this.setData({
        password:e.detail.value
      })
    }
  },
  goBack(){},
  LoginOrRegister(e){
    // this.data.username == '' ---> false
    if (!this.data.username.trim()) {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none'
      })
      return
    }
    if (!this.data.password.trim()) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return
    }

    // 请求接口
    var that = this;
    wx.request({
      url: 'https://mock.apifox.com/m1/3318905-0-default/login', //接口请求地址
      data: {
        userName: that.data.userName,
        passWord: that.data.password
      },
      header: {
        'content-type': 'application/json;charset=utf-8',
      },
      method: "POST",
      success: function(res) {
        // {msg.....}
        const responseData = res.data
        // ok 请求成功
        if (responseData.msg === 'ok') {
          if(that.data.isRegister) {
            that.setData({
              username: '',
              password: '',
              isRegister: !that.data.isRegister
            })
            wx.showToast({
              title: '注册成功，请登录',
              icon: 'none'
            })
          }else {
            // wx.switchTab({
            wx.redirectTo({
              url: '/pages/index/index',
            })      
          }
          return
        }
      },
      fail: function(err) {
        wx.showToast({
          title: "服务器出了点小差",
          icon: "none"
        });
      }
    })




  },
  ChangeShowType(){
    let str =  this.data.isRegister ? '1' : '0'
    console.log('str:',str);
    wx.redirectTo({
      url: '/pages/my/my?isRegister='+str,
    })
  }

})